﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace SmartMotion.Core
{
    public class ReflectAble:Attribute
    {
        /// <summary>
        /// 获取可用的类型，一个DLL里可能有很多类，类只添加了ReflectAble特性才能被反射
        /// </summary>
        /// <param name="dll"></param>
        /// <returns></returns>
        public static List<Type> GetTypes(string dll, bool loadAll = true)
        {

            Assembly curDll = Assembly.LoadFile(dll);
            var types = curDll.GetTypes();
            List<Type> AvailableType = new List<Type>();    
            foreach (var type in types)
            {
                if (loadAll)
                {
                    AvailableType.Add(type);
                }
                else
                {
                    //获得方法的特性
                    var attrList = type.GetCustomAttributes();
                    //枚举特性
                    foreach (var attr in attrList)
                    {
                        if (attr.GetType() == typeof(ReflectAble))//检查特性类型
                        {
                            AvailableType.Add(type);
                        }
                    }
                }
               
            }
            return AvailableType;
        }
        /// <summary>
        ///  获取可用的方法，一个类里可能有很多方法，类只添加了ReflectAble特性才能被反射
        /// </summary>
        /// <param name="objType"></param>
        /// <returns></returns>
        public static List<MethodInfo> GetMethods(Type objType)
        {
            List<MethodInfo> AvailableMethods = new List<MethodInfo>();
            var methods = objType.GetMethods();
            foreach (var mds in methods)
            {
                //获得方法的特性
                var attrList = mds.GetCustomAttributes();
                //枚举特性
                //foreach (var attr in attrList)
                {
                    //if (attr.GetType() == typeof(ReflectAble))//检查特性类型
                    AvailableMethods.Add(mds);
                }
            }
            return AvailableMethods;
        }
    }
}
